//
//  NSString+Additions.m
//  Gainko
//
//  Created by RanCheng.gu on 9/3/12.
//  Copyright (c) 2012 RanCheng.gu. All rights reserved.
//

#import "NSString+Additions.h"
#define kDEFAULT_DATE_TIME_FORMAT (@"MM/dd/yyyy")

#define BASE_PHOTO_URL @"nil"

@implementation NSString (Additions)

-(BOOL) isValidEmail{
    BOOL stricterFilter = YES; 
    NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSString *laxString = @".+@.+\\.[A-Za-z]{2}[A-Za-z]*";
    NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:self];
}

-(NSDate*) convertToDate{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    //
    [formatter setDateFormat:kDEFAULT_DATE_TIME_FORMAT];
    NSDate *date = [formatter dateFromString:self];
    return date;
}

+(NSString*) StringFromBoolean:(BOOL)result{
    NSString *str = @"NO";
    if(result){
       str = @"YES";
    }
    return str;
}

-(BOOL) isValidPhone{
    NSString * regex  = @"^[0-9]+$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL rs = [phoneTest evaluateWithObject:self];
    
    if(rs && (self.length>9 || self.length<12))
    {
        return YES;
    }else{
        return NO;
    }
}

-(BOOL)isValidPassword{
    /*NSString * regex  = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL rs = [phoneTest evaluateWithObject:self];
    if(rs)
    {
        return YES;
    }else{
        return NO;
    }*/
    if(self.length>=6 && self.length<=16)
    {
        return YES;
    }else{
        return NO;
    }
    
}

-(BOOL)isValidTradePassword{
    /*NSString * regex  = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
     NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
     BOOL rs = [phoneTest evaluateWithObject:self];
     if(rs)
     {
     return YES;
     }else{
     return NO;
     }*/
    if(self.length>=8 && self.length<=20)
    {
        return YES;
    }else{
        return NO;
    }
    
}

-(BOOL)isValidIDCardNO{
    
    NSString *regex = @"(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])";
    BOOL rs = NO;
    if(self && self.length==18)
    {
        NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        rs = [phoneTest evaluateWithObject:self];
    }
    
    return rs;
}

- (BOOL)containsString:(NSString *)aString
{
	NSRange range = [[self lowercaseString] rangeOfString:[aString lowercaseString]];
	return range.location != NSNotFound;
}

- (NSString*)telephoneWithReformat
{
    NSString * tempStr = self;
    if ([tempStr containsString:@"-"])
    {
        tempStr = [tempStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }
    
    if ([tempStr containsString:@" "])
    {
        tempStr = [tempStr stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
    
    if ([tempStr containsString:@"("])
    {
        tempStr = [tempStr stringByReplacingOccurrencesOfString:@"(" withString:@""];
    }
    
    if ([tempStr containsString:@")"])
    {
        tempStr = [tempStr stringByReplacingOccurrencesOfString:@")" withString:@""];
    }
    
    return tempStr;
}

-(NSString *)escapedWildcard
{
   NSString *tempStr =  [self stringByReplacingOccurrencesOfString:@"-" withString:@""];
    
    return tempStr;
}

-(NSURL *)fullImagePathUrl
{
    NSString *teampUrl = [NSString stringWithFormat:@"%@%@",BASE_PHOTO_URL,self];
    return [NSURL URLWithString:teampUrl];
}

-(NSString *)fullImagePath
{
    NSString *teampUrl = [NSString stringWithFormat:@"%@%@",BASE_PHOTO_URL,self];
    return teampUrl;
}

-(BOOL)isEmpty{
    BOOL isEmpty = NO;
    if (self == nil || [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length == 0) {
        isEmpty = YES;
    }
    return isEmpty;
}

@end
